package com.hskn.hss.module.worklog.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.domain.AjaxResult;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.depart.service.ISysDepartService;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.grid.mapper.GridMapper;
import com.hskn.hss.module.grid.service.IGridService;
import com.hskn.hss.module.rptCarWork.entity.RptCarWork;
import com.hskn.hss.module.rptCarWork.mapper.RptCarWorkMapper;
import com.hskn.hss.module.rptEmployeeWork.entity.RptEmployeeWork;
import com.hskn.hss.module.rptEmployeeWork.mapper.RptEmployeeWorkMapper;
import com.hskn.hss.module.tlcar.entity.TlCar;
import com.hskn.hss.module.tlcar.mapper.TlCarMapper;
import com.hskn.hss.module.tlcartype.entity.TlCarType;
import com.hskn.hss.module.tlcartype.service.ITlCarTypeService;
import com.hskn.hss.module.tlemployee.entity.TlEmployee;
import com.hskn.hss.module.tlemployee.mapper.TlEmployeeMapper;
import com.hskn.hss.module.tlfacilities.entity.TlFacilities;
import com.hskn.hss.module.tlfacilities.mapper.TlFacilitiesMapper;
import com.hskn.hss.module.tlfacilitietype.entity.TlFacilitiesType;
import com.hskn.hss.module.tlfacilitietype.service.ITlFacilitiesTypeService;
import com.hskn.hss.module.tlopetation.entity.TlOperation;
import com.hskn.hss.module.tlopetation.mapper.TlOperationMapper;
import com.hskn.hss.module.tlopetation.service.ITlOperationService;
import com.hskn.hss.module.tlworktype.entity.TlWorkType;
import com.hskn.hss.module.tlworktype.service.ITlWorkTypeService;
import com.hskn.hss.module.treemaintain.entity.TreeMaintain;
import com.hskn.hss.module.treemaintain.mapper.TreeMaintainMapper;
import com.hskn.hss.module.user.entity.User;
import com.hskn.hss.module.user.service.IUserService;
import com.hskn.hss.module.userdepart.service.ISysUserDepartService;
import com.hskn.hss.module.worklog.dto.InChargeSubjectDTO;
import com.hskn.hss.module.worklog.entity.WorkLog;
import com.hskn.hss.module.worklog.mapper.WorkLogMapper;
import com.hskn.hss.module.worklog.service.IWorkLogService;
import com.hskn.hss.module.worklog.to.AncientGardenTreesTO;
import com.hskn.hss.module.worklog.to.JobLogExportTO;
import com.hskn.hss.module.worklog.vo.*;
import com.hskn.hss.utils.StringUtils;
import com.hskn.hss.utils.Tools;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-19
 */
@Service
public class WorkLogServiceImpl extends ServiceImpl<WorkLogMapper, WorkLog> implements IWorkLogService {

    @Resource
    private WorkLogMapper workLogMapper;

    @Resource
    private ISysUserDepartService sysUserDepartService;

    @Resource
    private ISysDepartService sysDepartService;
    @Resource
    private TlEmployeeMapper employeeMapper;
    @Resource
    private GridMapper gridMapper;
    @Resource
    private IGridService gridService;
    @Resource
    private TlCarMapper carMapper;
    @Resource
    private IUserService userService;
    @Resource
    private RptCarWorkMapper rptCarWorkMapper;
    @Resource
    private RptEmployeeWorkMapper rptEmployeeWorkMapper;
    @Resource
    private TlOperationMapper tlOperationMapper;
    @Resource
    private ITlOperationService operationService;
    @Resource
    private ITlWorkTypeService workTypeService;
    @Resource
    private ITlCarTypeService carTypeService;
    @Resource
    private ITlFacilitiesTypeService facilitiesTypeService;
    @Resource
    private TlFacilitiesMapper facilitiesMapper;
    @Resource
    private TreeMaintainMapper treeMaintainMapper;


    @Override
    public JSONObject mList(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        JSONObject json = new JSONObject();
        QueryWrapper<WorkLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(WorkLog::getCreateTime);
        queryWrapper.lambda().eq(WorkLog::getCreateUid, uid);
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            queryWrapper.lambda().between(WorkLog::getCreateTime, startDate, endDate);
        }
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        Page<WorkLog> workLogPage = new Page<>(page, size, true);
        IPage<WorkLog> iPage = workLogMapper.selectPage(workLogPage, queryWrapper);
        //2021/11/11 追加：作业类型名添加
        List<WorkLog> workLogs = iPage.getRecords();
        if (workLogs.size() > 0) {
            //从作业类型表中查询类型名返回
            List<TlOperation> tlOperations = tlOperationMapper.selectList(null);
            workLogs.forEach(workLog -> {
                tlOperations.forEach(tlOperation -> {
                    if (tlOperation.getId().equals(workLog.getJobTypeId())) {
                        workLog.setJobTypeName(tlOperation.getOperationtype());
                    }
                });
            });
        }
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("workLogs", workLogs);
        return json;
    }

    /**
     * 查询作业日志列表
     *
     * @param jobLogExportTO
     * @return java.util.List<com.hskn.hss.module.worklog.entity.WorkLog>
     * @author zcy
     * @date 2021/11/8 10:43
     */
    private List<WorkLog> selectList(JobLogExportTO jobLogExportTO) {
        Date startDate = jobLogExportTO.getStartTime();
        Date endDate = jobLogExportTO.getEndTime();
        String userId = jobLogExportTO.getUserId();
        String subjectId = jobLogExportTO.getSubjectId();
        String gridId = jobLogExportTO.getGridId();
        Integer logType = jobLogExportTO.getLogType();
        QueryWrapper<WorkLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(WorkLog::getCreateTime);
        if (Objects.nonNull(startDate) && Objects.nonNull(endDate)) {
            queryWrapper.lambda().between(WorkLog::getCreateTime, startDate, endDate);
        }
        if (CollectionUtils.isNotEmpty(jobLogExportTO.getIds())) {
            queryWrapper.lambda().in(WorkLog::getWorkLogId, jobLogExportTO.getIds());
        }
        if (StringUtils.isNotEmpty(subjectId)) {
            queryWrapper.lambda().eq(WorkLog::getSubjectId, subjectId);
        }
        if (StringUtils.isNotEmpty(userId)) {
            queryWrapper.lambda().eq(WorkLog::getCreateUid, userId);
        }
        if (StringUtils.isNotEmpty(gridId)) {
            queryWrapper.lambda().eq(WorkLog::getGridId, gridId);
        }
        if (Objects.nonNull(logType)) {
            queryWrapper.lambda().eq(WorkLog::getLogType, logType);
        }
        return workLogMapper.selectList(queryWrapper);
    }

    /**
     * 查询园林古树养护列表
     *
     * @param ancientGardenTreesTO
     * @return java.util.List<com.hskn.hss.module.worklog.entity.WorkLog>
     * @author zcy
     * @date 2021/11/8 10:43
     */
    private List<TreeMaintain> selectList(AncientGardenTreesTO ancientGardenTreesTO) {
        Date startDate = ancientGardenTreesTO.getStartTime();
        Date endDate = ancientGardenTreesTO.getEndTime();
        String userId = ancientGardenTreesTO.getUserId();
        String subjectName = ancientGardenTreesTO.getSubjectName();
        Integer logType = ancientGardenTreesTO.getLogType();
        QueryWrapper<TreeMaintain> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(TreeMaintain::getCreateTime);
        if (Objects.nonNull(startDate) && Objects.nonNull(endDate)) {
            queryWrapper.lambda().between(TreeMaintain::getCreateTime, startDate, endDate);
        }
        if (CollectionUtils.isNotEmpty(ancientGardenTreesTO.getIds())) {
            queryWrapper.lambda().in(TreeMaintain::getMaintainId, ancientGardenTreesTO.getIds());
        }
        if (StringUtils.isNotEmpty(subjectName)) {
            List<TlFacilities> facilitiesList = facilitiesMapper.selectList(new QueryWrapper<TlFacilities>().lambda().like(TlFacilities::getFaclname,subjectName));
            if (CollectionUtils.isNotEmpty(facilitiesList)) {
                queryWrapper.lambda().in(TreeMaintain::getTreeId, facilitiesList.stream().map(TlFacilities::getId));
            }
        }
        if (StringUtils.isNotEmpty(userId)) {
            queryWrapper.lambda().eq(TreeMaintain::getCreateUid, userId);
        }
        if (Objects.nonNull(logType)) {
            queryWrapper.lambda().eq(TreeMaintain::getLogType, logType);
        }
        return treeMaintainMapper.selectList(queryWrapper);
    }

    /**
     * 车辆作业日志处理
     *
     * @param jobLogExportTO
     * @return java.util.List<com.hskn.hss.module.worklog.vo.JobLogExportCarExcelVO>
     * @author zcy
     * @date 2021/11/8 16:58
     */
    @Override
    public List<JobLogExportCarExcelVO> selectCarWorkLogList(JobLogExportTO jobLogExportTO) {
        jobLogExportTO.setLogType(1);
        List<WorkLog> workLogList = selectList(jobLogExportTO);
        List<JobLogExportCarExcelVO> jobLogExportCarExcelVOList = new ArrayList<>();
        Map<String, User> userMap = userService.getUserMap();
        Map<String, SysDepart> departMap = sysDepartService.getDepartMap(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridService.getGridMap(new QueryWrapper<>());
        Map<String, TlOperation> operationMap = operationService.getTlOperationMap(new QueryWrapper<>());
        for (WorkLog workLog : workLogList) {
            jobLogExportCarExcelVOList.add(buildJobLogExportCarExcelVO(workLog, userMap, departMap, gridMap,operationMap));
        }
        return jobLogExportCarExcelVOList;
    }

    /**
     * 车辆作业日志构造
     *
     * @param workLog
     * @return com.hskn.hss.module.worklog.vo.JobLogExportCarExcelVO
     * @author zcy
     * @date 2021/11/8 16:59
     */
    private JobLogExportCarExcelVO buildJobLogExportCarExcelVO(WorkLog workLog, Map<String, User> userMap, Map<String, SysDepart> departMap
            , Map<String, Grid> gridMap, Map<String,TlOperation> operationMap) {

        JobLogExportCarExcelVO jobLogExportCarExcelVO = new JobLogExportCarExcelVO();
        if (StringUtils.isNotEmpty(workLog.getCreateUid()) && userMap.containsKey(workLog.getCreateUid())) {
            jobLogExportCarExcelVO.setSubmitter(userMap.get(workLog.getCreateUid()).getName());
        }
        if (StringUtils.isNotEmpty(workLog.getDepid()) && departMap.containsKey(workLog.getDepid())) {
            jobLogExportCarExcelVO.setDepname(departMap.get(workLog.getDepid()).getDepartName());
        }
        jobLogExportCarExcelVO.setId(workLog.getWorkLogId());
        jobLogExportCarExcelVO.setLogContent(workLog.getContent());
        jobLogExportCarExcelVO.setImgurls(workLog.getImgurls());
        jobLogExportCarExcelVO.setVideourl(workLog.getVideourl());
        if (gridMap.containsKey(workLog.getGridId())) {
            jobLogExportCarExcelVO.setJobGrid(gridMap.get(workLog.getGridId()).getName());
        }
        jobLogExportCarExcelVO.setJobCar(workLog.getSubjectName());
        if (operationMap.containsKey(workLog.getJobTypeId())) {
            jobLogExportCarExcelVO.setJobType(operationMap.get(workLog.getJobTypeId()).getOperationtype());
        }
        jobLogExportCarExcelVO.setSubmitTime(workLog.getCreateTime());
        return jobLogExportCarExcelVO;
    }

    /**
     * 人员作业日志处理
     *
     * @param jobLogExportTO
     * @return java.util.List<com.hskn.hss.module.worklog.vo.JobLogExportCarExcelVO>
     * @author zcy
     * @date 2021/11/8 16:58
     */
    @Override
    public List<JobLogExportPersonExcelVO> selectPersonWorkLogList(JobLogExportTO jobLogExportTO) {
        
        jobLogExportTO.setLogType(2);
        List<WorkLog> workLogList = selectList(jobLogExportTO);
        List<JobLogExportPersonExcelVO> jobLogExportCarExcelVOList = new ArrayList<>();
        Map<String, User> userMap = userService.getUserMap();
        Map<String, SysDepart> departMap = sysDepartService.getDepartMap(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridService.getGridMap(new QueryWrapper<>());
        Map<String, TlOperation> operationMap = operationService.getTlOperationMap(new QueryWrapper<>());
        for (WorkLog workLog : workLogList) {
            jobLogExportCarExcelVOList.add(buildJobLogExportPersonExcelVO(workLog, userMap, departMap, gridMap,operationMap));
        }
        return jobLogExportCarExcelVOList;
    }

    /**
     * 人员作业日志构造
     *
     * @param workLog
     * @return com.hskn.hss.module.worklog.vo.JobLogExportCarExcelVO
     * @author zcy
     * @date 2021/11/8 16:59
     */
    private JobLogExportPersonExcelVO buildJobLogExportPersonExcelVO(WorkLog workLog, Map<String, User> userMap, Map<String, SysDepart> departMap,
                                                                     Map<String, Grid> gridMap,Map<String, TlOperation> operationMap) {

        JobLogExportPersonExcelVO jobLogExportPersonExcelVO = new JobLogExportPersonExcelVO();
        if (StringUtils.isNotEmpty(workLog.getCreateUid()) && userMap.containsKey(workLog.getCreateUid())) {
            jobLogExportPersonExcelVO.setSubmitter(userMap.get(workLog.getCreateUid()).getName());
        }
        if (StringUtils.isNotEmpty(workLog.getDepid()) && departMap.containsKey(workLog.getDepid())) {
            jobLogExportPersonExcelVO.setDepname(departMap.get(workLog.getDepid()).getDepartName());
        }
        jobLogExportPersonExcelVO.setId(workLog.getWorkLogId());
        jobLogExportPersonExcelVO.setLogContent(workLog.getContent());
        jobLogExportPersonExcelVO.setImgurls(workLog.getImgurls());
        jobLogExportPersonExcelVO.setVideourl(workLog.getVideourl());
        if (gridMap.containsKey(workLog.getGridId())) {
            jobLogExportPersonExcelVO.setJobGrid(gridMap.get(workLog.getGridId()).getName());
        }
        jobLogExportPersonExcelVO.setJobPerson(workLog.getSubjectName());
        if (operationMap.containsKey(workLog.getJobTypeId())) {
            jobLogExportPersonExcelVO.setJobType(operationMap.get(workLog.getJobTypeId()).getOperationtype());
        } else {
            jobLogExportPersonExcelVO.setJobType("道路保洁");
        }
        jobLogExportPersonExcelVO.setSubmitTime(workLog.getCreateTime());
        return jobLogExportPersonExcelVO;
    }

    /**
     * 设施作业日志处理
     *
     * @param jobLogExportTO
     * @return java.util.List<com.hskn.hss.module.worklog.vo.JobLogExportCarExcelVO>
     * @author zcy
     * @date 2021/11/8 16:58
     */
    @Override
    public List<JobLogExportFacExcelVO> selectFacWorkLogList(JobLogExportTO jobLogExportTO) {

        jobLogExportTO.setLogType(3);
        List<WorkLog> workLogList = selectList(jobLogExportTO);
        List<JobLogExportFacExcelVO> jobLogExportCarExcelVOList = new ArrayList<>();
        Map<String, User> userMap = userService.getUserMap();
        Map<String, SysDepart> departMap = sysDepartService.getDepartMap(new QueryWrapper<>());
        Map<String, Grid> gridMap = gridService.getGridMap(new QueryWrapper<>());
        for (WorkLog workLog : workLogList) {
            jobLogExportCarExcelVOList.add(buildJobLogExportFacExcelVO(workLog, userMap, departMap,gridMap));
        }
        return jobLogExportCarExcelVOList;
    }

    /**
     * 设施作业日志构造
     *
     * @param workLog
     * @return com.hskn.hss.module.worklog.vo.JobLogExportCarExcelVO
     * @author zcy
     * @date 2021/11/8 16:59
     */
    private JobLogExportFacExcelVO buildJobLogExportFacExcelVO(WorkLog workLog, Map<String, User> userMap, Map<String, SysDepart> departMap ,Map<String, Grid> gridMap) {

        JobLogExportFacExcelVO jobLogExportFacExcelVO = new JobLogExportFacExcelVO();
        if (StringUtils.isNotEmpty(workLog.getCreateUid()) && userMap.containsKey(workLog.getCreateUid())) {
            jobLogExportFacExcelVO.setSubmitter(userMap.get(workLog.getCreateUid()).getName());
        }
        if (StringUtils.isNotEmpty(workLog.getDepid()) && departMap.containsKey(workLog.getDepid())) {
            jobLogExportFacExcelVO.setDepname(departMap.get(workLog.getDepid()).getDepartName());
        }
        jobLogExportFacExcelVO.setId(workLog.getWorkLogId());
        jobLogExportFacExcelVO.setLogContent(workLog.getContent());
        jobLogExportFacExcelVO.setImgurls(workLog.getImgurls());
        jobLogExportFacExcelVO.setVideourl(workLog.getVideourl());
        if (gridMap.containsKey(workLog.getGridId())) {
            jobLogExportFacExcelVO.setJobGrid(gridMap.get(workLog.getGridId()).getName());
        }
        jobLogExportFacExcelVO.setFacName(workLog.getSubjectName());
        jobLogExportFacExcelVO.setSubmitTime(workLog.getCreateTime());
        return jobLogExportFacExcelVO;
    }

    /**
     * 园林作业日志处理
     *
     * @param ancientGardenTreesTO
     * @return java.util.List<com.hskn.hss.module.worklog.vo.JobLogExportCarExcelVO>
     * @author zcy
     * @date 2021/11/8 16:58
     */
    @Override
    public List<JobLogExportGardenExcelVO> selectGarWorkLogList(AncientGardenTreesTO ancientGardenTreesTO) {
        ancientGardenTreesTO.setLogType(1);
        List<TreeMaintain> treeMaintainList = selectList(ancientGardenTreesTO);
        List<JobLogExportGardenExcelVO> jobLogExportCarExcelVOList = new ArrayList<>();
        Map<String, User> userMap = userService.getUserMap();
        Map<String, SysDepart> departMap = sysDepartService.getDepartMap(new QueryWrapper<>());
        for (TreeMaintain treeMaintain : treeMaintainList) {
            jobLogExportCarExcelVOList.add(buildJobLogExportGardenExcelVO(treeMaintain, userMap, departMap));
        }
        return jobLogExportCarExcelVOList;
    }

    /**
     * 园林作业日志构造
     *
     * @param treeMaintain
     * @param userMap
     * @param departMap
     * @return com.hskn.hss.module.worklog.vo.JobLogExportCarExcelVO
     * @author zcy
     * @date 2021/11/8 16:59
     */
    private JobLogExportGardenExcelVO buildJobLogExportGardenExcelVO(TreeMaintain treeMaintain,
                                                                     Map<String, User> userMap, Map<String, SysDepart> departMap) {
        JobLogExportGardenExcelVO jobLogExportFacExcelVO = new JobLogExportGardenExcelVO();
        if (StringUtils.isNotEmpty(treeMaintain.getCreateUid()) && userMap.containsKey(treeMaintain.getCreateUid())) {
            jobLogExportFacExcelVO.setSubmitter(userMap.get(treeMaintain.getCreateUid()).getName());
        }
        if (StringUtils.isNotEmpty(treeMaintain.getDepid()) && departMap.containsKey(treeMaintain.getDepid())) {
            jobLogExportFacExcelVO.setDepname(departMap.get(treeMaintain.getDepid()).getDepartName());
        }
        jobLogExportFacExcelVO.setId(treeMaintain.getMaintainId());
        jobLogExportFacExcelVO.setLogContent(treeMaintain.getDes());
        jobLogExportFacExcelVO.setImgurls(treeMaintain.getImgurls());
        jobLogExportFacExcelVO.setVideourl(treeMaintain.getVideourl());
        jobLogExportFacExcelVO.setTypeName(treeMaintain.getTypeName());
        jobLogExportFacExcelVO.setGarName(treeMaintain.getTreeName());
        jobLogExportFacExcelVO.setSubmitTime(treeMaintain.getCreateTime());
        return jobLogExportFacExcelVO;
    }

    /**
     * 古树作业日志处理
     *
     * @param ancientGardenTreesTO
     * @return java.util.List<com.hskn.hss.module.worklog.vo.JobLogExportCarExcelVO>
     * @author zcy
     * @date 2021/11/8 16:58
     */
    @Override
    public List<JobLogExportTreeExcelVO> selectTreeWorkLogList(AncientGardenTreesTO ancientGardenTreesTO) {
        ancientGardenTreesTO.setLogType(2);
        List<TreeMaintain> workLogList = selectList(ancientGardenTreesTO);
        List<JobLogExportTreeExcelVO> jobLogExportCarExcelVOList = new ArrayList<>();
        Map<String, User> userMap = userService.getUserMap();
        Map<String, SysDepart> departMap = sysDepartService.getDepartMap(new QueryWrapper<>());
        for (TreeMaintain workLog : workLogList) {
            jobLogExportCarExcelVOList.add(buildJobLogExportTreeExcelVO(workLog, userMap, departMap));
        }
        return jobLogExportCarExcelVOList;
    }

    /**
     * 古树作业日志构造
     *
     * @param treeMaintain
     * @return com.hskn.hss.module.worklog.vo.JobLogExportCarExcelVO
     * @author zcy
     * @date 2021/11/8 16:59
     */
    private JobLogExportTreeExcelVO buildJobLogExportTreeExcelVO(TreeMaintain treeMaintain, Map<String, User> userMap, Map<String, SysDepart> departMap) {
        JobLogExportTreeExcelVO jobLogExportFacExcelVO = new JobLogExportTreeExcelVO();
        if (StringUtils.isNotEmpty(treeMaintain.getCreateUid()) && userMap.containsKey(treeMaintain.getCreateUid())) {
            jobLogExportFacExcelVO.setSubmitter(userMap.get(treeMaintain.getCreateUid()).getName());
        }
        if (StringUtils.isNotEmpty(treeMaintain.getDepid()) && departMap.containsKey(treeMaintain.getDepid())) {
            jobLogExportFacExcelVO.setDepname(departMap.get(treeMaintain.getDepid()).getDepartName());
        }
        jobLogExportFacExcelVO.setId(treeMaintain.getMaintainId());
        jobLogExportFacExcelVO.setLogContent(treeMaintain.getDes());
        jobLogExportFacExcelVO.setImgurls(treeMaintain.getImgurls());
        jobLogExportFacExcelVO.setVideourl(treeMaintain.getVideourl());
        jobLogExportFacExcelVO.setTreeName(treeMaintain.getTreeName());
        jobLogExportFacExcelVO.setTypeName(treeMaintain.getTypeName());
        jobLogExportFacExcelVO.setSubmitTime(treeMaintain.getCreateTime());
        return jobLogExportFacExcelVO;
    }

    @Override
    public AjaxResult listById(Map<String, Object> map) {
        List<WorkLog> workLogs = workLogMapper.selectForRevoke(map);
        return AjaxResult.success(Tools.getDataTable(workLogs));
    }

    @Override
    public JSONObject rList(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        JSONObject json = new JSONObject();
        if (params == null) {
            params = new JSONObject();
        }
        QueryWrapper<WorkLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(WorkLog::getCreateTime);
        queryWrapper.lambda().like(WorkLog::getLeaderUids, uid);
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            queryWrapper.lambda().between(WorkLog::getCreateTime, startDate, endDate);
        }
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        Page<WorkLog> workLogPage = new Page<>(page, size, true);
        IPage<WorkLog> iPage = workLogMapper.selectPage(workLogPage, queryWrapper);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("workLogs", iPage.getRecords());
        return json;
    }

    @Override
    @Transactional
    public void mUpdate(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("workLogId"))) {
            throw new HssException("参数错误");
        }
        WorkLog workLog = JSON.parseObject(params.toJSONString(), WorkLog.class);
        workLogMapper.updateById(workLog);
    }

    @Override
    @Transactional
    public void add(WorkLog workLog) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        workLog.setCreateUid(uid);
        workLog.setDepid(sysUserDepartService.getDeptidByUid(uid));
        workLog.setLeaderUids(sysDepartService.getLeaderUids());
        TlEmployee employee = new TlEmployee();
        employee.setBindaccount(uid);
        TlCar car = new TlCar();
        car.setBindaccount(uid);
        List<TlEmployee> employeeList = employeeMapper.selectList(new QueryWrapper<>(employee));
        List<TlCar> carList = carMapper.selectList(new QueryWrapper<>(car));
        String gridId = null;
        if (!employeeList.isEmpty()) {
            QueryWrapper<RptEmployeeWork> employeeWorkQueryWrapper = new QueryWrapper<>();
            employeeWorkQueryWrapper.lambda().eq(RptEmployeeWork::getEmployeeId, employeeList.get(0).getId());
            employeeWorkQueryWrapper.lambda().orderByDesc(RptEmployeeWork::getCreateTime);
            employeeWorkQueryWrapper.lambda().last(" LIMIT 1");
            RptEmployeeWork rptEmployeeWork = rptEmployeeWorkMapper.selectOne(employeeWorkQueryWrapper);
            workLog.setGridId(rptEmployeeWork.getGridId());
            gridId = rptEmployeeWork.getGridId();
        } else if (!carList.isEmpty()) {
            QueryWrapper<RptCarWork> rptCarWorkQueryWrapper = new QueryWrapper<RptCarWork>();
            rptCarWorkQueryWrapper.lambda().eq(RptCarWork::getCarId, carList.get(0).getId());
            rptCarWorkQueryWrapper.lambda().orderByDesc(RptCarWork::getCreateTime);
            rptCarWorkQueryWrapper.lambda().last(" LIMIT 1");
            RptCarWork carWork = rptCarWorkMapper.selectOne(rptCarWorkQueryWrapper);
            workLog.setGridId(carWork.getGridId());
            gridId = carWork.getGridId();
        }
        if (StringUtils.isNotEmpty(gridId)) {
            Grid grid = gridMapper.selectById(gridId);
            workLog.setGridName(grid.getName());
        }
        List<InChargeSubjectDTO> inChargeSubjectDTOList = getPersonChargeSubject(uid);
        inChargeSubjectDTOList = inChargeSubjectDTOList.stream().filter(inChargeSubjectDTO->inChargeSubjectDTO.getType()>4).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(inChargeSubjectDTOList)) {
            workLog.setSubjectId(inChargeSubjectDTOList.get(0).getSubjectId());
            workLog.setSubjectName(inChargeSubjectDTOList.get(0).getSubjectName());
            workLog.setSubjectType(inChargeSubjectDTOList.get(0).getSubjectTypeId());
            workLog.setJobTypeId(inChargeSubjectDTOList.get(0).getSubjectWorkTypeId());
            workLog.setLogType(inChargeSubjectDTOList.get(0).getType());
        }
        workLogMapper.insert(workLog);
    }

    /**
     * 获取账号绑定的车辆、人员、设施
     *
     * @param personId 账号ID
     * @return List<InChargeSubjectDTO> 账号绑定的车辆、人员、设施
     */
    @Override
    public List<InChargeSubjectDTO> getPersonChargeSubject(String personId) {
        List<InChargeSubjectDTO> inChargeSubjectDTOList = new ArrayList<>();
        Map<String, SysDepart> departMap = sysDepartService.getDepartMap(new QueryWrapper<>());
        QueryWrapper<TlCar> carQueryWrapper = new QueryWrapper<>();
        carQueryWrapper.lambda().eq(TlCar::getBindaccount, personId);
        List<TlCar> carList = carMapper.selectList(carQueryWrapper);
        if (CollectionUtils.isNotEmpty(carList)) {
            Map<String, TlCarType> carTypeMap = carTypeService.getCarTypeMap(new QueryWrapper<>());
            Map<String, TlOperation> operationMap = operationService.getTlOperationMap(new QueryWrapper<>());
            for (TlCar tlCar : carList) {
                inChargeSubjectDTOList.add(InChargeSubjectDTO.buildInChargeSubjectDTO(tlCar, carTypeMap, operationMap, departMap));
            }
        }
        QueryWrapper<TlFacilities> facilitiesQueryWrapper = new QueryWrapper<>();
        facilitiesQueryWrapper.lambda().eq(TlFacilities::getPrincipal, personId);
        List<TlFacilities> facilitiesList = facilitiesMapper.selectList(facilitiesQueryWrapper);
        if (CollectionUtils.isNotEmpty(facilitiesList)) {
            Map<String, TlFacilitiesType> facilitiesTypeMap = facilitiesTypeService.getTlFacilitiesTypeMap(new QueryWrapper<>());
            for (TlFacilities tlFacilities : facilitiesList) {
                if (StringUtils.isNotEmpty(tlFacilities.getFacltypename()) && tlFacilities.getFacltypename().equals("园林绿化")) {
                    inChargeSubjectDTOList.add(InChargeSubjectDTO.buildInChargeSubjectDTO(tlFacilities, 4, facilitiesTypeMap, departMap));
                } else if (StringUtils.isNotEmpty(tlFacilities.getFacltypename()) && tlFacilities.getFacltypename().equals("名木古树")) {
                    inChargeSubjectDTOList.add(InChargeSubjectDTO.buildInChargeSubjectDTO(tlFacilities, 5, facilitiesTypeMap, departMap));
                } else {
                    inChargeSubjectDTOList.add(InChargeSubjectDTO.buildInChargeSubjectDTO(tlFacilities, 3, facilitiesTypeMap, departMap));
                }

            }
        }
        QueryWrapper<TlEmployee> employeeQueryWrapper = new QueryWrapper<>();
        employeeQueryWrapper.lambda().eq(TlEmployee::getBindaccount, personId);
        List<TlEmployee> employeeList = employeeMapper.selectList(employeeQueryWrapper);
        if (CollectionUtils.isNotEmpty(employeeList)) {
            Map<String, TlWorkType> workTypeMap = workTypeService.getTlWorkTypeMap(new QueryWrapper<>());
            for (TlEmployee tlEmployee : employeeList) {
                inChargeSubjectDTOList.add(InChargeSubjectDTO.buildInChargeSubjectDTO(tlEmployee, workTypeMap, departMap));
            }
        }
        return inChargeSubjectDTOList;
    }

    @Override
    public void del(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("workLogId"))) {
            throw new HssException("参数错误");
        }
        String workLogId = params.getString("workLogId");
        workLogMapper.deleteById(workLogId);
    }

    @Override
    public void revoke(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        if (StringUtils.isEmpty(params) || StringUtils.isEmpty(params.getString("workLogId"))) {
            throw new HssException("参数错误");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("uId", uid);
        map.put("nowtime", new Date());
        map.put("isRevoke", 2);
//        List<WorkLog> ru=workLogMapper.selectForRevoke(map);
//        if (ru!=null&&ru.size()>0) {
//            throw new HssException("工作日志不可多次撤销");
//        }
        String workLogId = params.getString("workLogId");
        WorkLog workLog = workLogMapper.selectById(workLogId);
        if (!isToday(workLog.getCreateTime())) {
            throw new HssException("撤销失败");
        }
        workLog.setIsRevoke(2);
        workLogMapper.updateById(workLog);
    }

    private Boolean isToday(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        String createTime = format.format(date);
        String now = format.format(new Date());
        return createTime.equals(now);
    }

}
